<?php
/**
 * Created by PhpStorm.
 * User: WangXiaoHong
 * Date: 15-11-23
 * Time: 下午15:23
 */
$txt=file_get_contents('php://input');
include_once ('/var/www/html/new/d/module/wxh/nm/fun.class.php');
err($txt,getip().'|send','msg_record.txt');
$ac1='16ucom';
$pw1='c33367701511b4f6020ec61ded352059';
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac1|pw_16u=$pw|auth_16u=true"));

$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
$ac='124625'; //百度糯米接口账号
$pw='a44e45409a99d71088bea2c12314a85c';
parse_str($txt,$date); //转换数据
 if(empty($date['custId']) || empty($date['encode'])|| empty($date['time'])  ||
    empty($date['orderId'])|| empty($date['count']) || empty($date['mobile'])||
    empty($date['productId'])){
    $txt='参数错误';
    err($txt,getip(),'send_fail.txt');
     /****参数错误****/
    ret_json($status=2,$orderId="",$coupon="",$ext="");
    exit;
}
$custId = '124625'; //二维码商帐号
$p_encode = 'a44e45409a99d71088bea2c12314a85c'; //二维码商密钥
$time = $date['time']; //获取时间戳

$encode = md5($p_encode.$time.$custId); //加密签名
//判断签名是否相等
if($date['encode'] == $encode)  {
    $id_list=explode('|',$date['productId']);
    $aid=$id_list[0];
    $lid=$id_list[1];
    $tid=$id_list[2];
    $UUremotenum = $date['orderId']; //远端订单号  注：是thirdOrderId 和 thirdTicketId的合并，回调的时候会用到
    $price = 100;    // 价格
    $num = $date['count']; //数量
    if(strlen($date['time'])>10) $playtime=date('Y-m-d',substr($date['time'],0,10)); //游玩日期
    else{
        $playtime=date('Y-m-d',$date['time']);
    }
    $tel = $date['mobile']; //游客手机
    //这里是下单程序
    $send_array =array("ac"=>$ac,"pw"=>$pw,"in0"=>$lid,"in1"=>$tid,"in2"=>$UUremotenum,"in3"=>$price,"in4"=>1,"in5"=>$playtime,"in6"=>'百度糯米',"in7"=>$tel,"in8"=>"","in9"=>0,"in10"=>2,"in11"=>0,"in12"=>"","in13"=>"","in14"=>0,"in15"=>0,"in16"=>$aid);
    $xml=$s1->__soapCall("PFT_Order_Submit",$send_array);
    $result=simpleXML_load_string($xml);
    try
    {
        if(isset($result->Rec->UUerrorcode)){
            //记录入文件中
            $txt=$UUremotenum.'|'.(string)$result->Rec->UUerrorcode;
            if((int)$result->Rec->UUerrorcode==122){
                //授信不足，提醒供应商给分销商授信
                $str="select mobile from pft_member where id='$landid' limit 1";
                $GLOBALS['le']->query($str);
                $GLOBALS['le']->fetch_assoc();
                $aid_Tel=$GLOBALS['le']->f('mobile');
                if($aid_Tel){
                    $s->Send_SMS_V($aid_Tel,"系统公告:您的分销商[百度糯米网]因授信不足下单失败，失败订单号:".$UUremotenum.",请尽快补充授信后联系OTA商家补单，避免影响销量");
                }
            }elseif((int)$result->Rec->UUerrorcode==1075){
                err($txt.'|'.(string)$result->Rec->UUordernum.'|'.(string)$result->Rec->UUcode,getip(),'send_fail.txt');
                /**远端订单号重复**/
                ret_json($status=1,$orderId=(string)$result->Rec->UUordernum,$coupon=(string)$result->Rec->UUcode,$ext="");
                exit;
            }
            err($txt,getip(),'send_fail.txt');
            ret_json($status=2,$orderId="",$coupon="",$ext="");
        }
        else{
            //成功
            $code_array=array("ac"=>$ac,"pw"=>$pw,"in0"=>(string)$result->Rec->UUordernum);
            $code=simpleXML_load_string($s1->__soapCall("Terminal_Code_Verify",$code_array));
            $txt=$UUremotenum.'|'.(string)$result->Rec->UUordernum.'|'.$code;
            err($txt,getip(),'send_success.txt');
            ret_json($status=0,$orderId=(string)$result->Rec->UUordernum,$coupon=(string)$code,$ext="");
        }
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }
}else {
    /***encode错误***/
    $status=3;
    ret_json($status,$orderId="",$coupon="",$ext="");
}
